﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using starLiGHT.Collections;
using Microsoft.Xna.Framework;

namespace starLiGHT.Engine.Tests
{
    public class PoolObject
    {
        public int Value;

        public PoolObject() { }
        public PoolObject(int value) { Value = value; }
    }

    public class TestPool : StaticPool<PoolObject>
    {
        public TestPool()
            : base()
        {
        }

        public TestPool(int capacity)
            : base(capacity)
        {
        }

        protected override PoolObject AllocateNew()
        {
            return new PoolObject();
        }
    }

    [TestFixture]
    public class StaticPool
    {
        TestPool pool;

        [SetUp]
        public void Init()
        {
            pool = new TestPool();
        }

        [Test]
        public void Constructor01()
        {
            Assert.AreEqual(512, pool.PoolCapacity);
        }

        [Test]
        public void Allocate01()
        {
            pool = new TestPool(5);

            Assert.AreEqual(5, pool.PoolCapacity);

            for (int i = 0; i < 5; i++)
            {
                PoolObject po = pool.Allocate();
            }
        }

        [Test]
        public void Release01()
        {
            pool = new TestPool(5);

            Assert.AreEqual(5, pool.PoolCapacity);

            for (int i = 0; i < 5; i++)
            {
                PoolObject po = pool.Allocate();
                po.Value = i;
                pool.Release(po);
            }

            for (int i = 0; i < 5; i++)
            {
                PoolObject po = pool.Allocate();
                Assert.LessOrEqual(po.Value, 4);
            }
        }
    }
}
